<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },
        purchaseProductId: {
          label: '采购订单',
          type: 'list',
          list: [],
          filterable: true,
          placeholder: '采购订单',
          selectCallback: this.orderDetailIdChanged
        },
        productId: {
          label: '产品',
          type: 'list',
          list: [],
          filterable: true,
          placeholder: '请选择产品',
          isNeedAdd: true,
          addNewItemFunc: this.addNewProduct,
          selectCallback: this.productChanged
        },
        supplierId: {
          label: '供应商名称',
          type: 'list',
          list: [],
          filterable: true,
          placeholder: '请选择供应商'
        },
        businessId:{
          label: '业务人员',
          type: 'list',
          list: [],
          filterable: true,
          placeholder: '请选择业务员人员'
        },
        customerRequire:{
          label: '规格描述',
        },
        productWeight: {
          label: '克数',
          type: 'number',
          max: 1000,
          default: 0,
          selectCallback: this.productWeightChanged
        },
        boxSupplyWay: {
          label: '包装方式',
          type: 'radio',
          radioBorder: true,
          radioList: [
            {text: '纸箱', label: 0},
          ],
          default: 0,
          selectCallback: this.numberChanged
        },
        // trayInfoId: {
        //   label: '托盘',
        //   type: 'list',
        //   placeholder: '请选择托盘',
        //   filterable: true,
        //   clearable:true,
        //   list: [],
        //   reply: () => this.R.equals(1, this.dataForm['boxSupplyWay'])
        // },
        trayNumber: {
          label: '托数',
          type: 'number',
          max: 100000,
          default: 0,
          reply: () => this.R.equals(1, this.dataForm['boxSupplyWay']),
          selectCallback: this.numberChanged
        },
        trayLayerNumber: {
          label: '托盘层数',
          type: 'number',
          max: 10000,
          default: 0,
          reply: () => this.R.equals(1, this.dataForm['boxSupplyWay']),
          selectCallback: this.numberChanged
        },
        trayEachZhiNumber: {
          label: '托盘每层只数',
          type: 'number',
          max: 10000,
          default: 0,
          reply: () => this.R.equals(1, this.dataForm['boxSupplyWay']),
          selectCallback: this.numberChanged
        },
        // boxId: {
        //   label: '纸箱',
        //   type: 'list',
        //   placeholder: '请选择纸箱',
        //   filterable: true,
        //   clearable:true,
        //   list: [],
        //   reply: () => this.R.equals(0, this.dataForm['boxSupplyWay'])
        // },
        boxNumber: {
          label: '箱数',
          type: 'number',
          max: 1000000,
          default: 0,
          reply: () => this.R.equals(0, this.dataForm['boxSupplyWay']),
          selectCallback: this.numberChanged
        },
        boxZhiNumber: {
          label: '只数',
          type: 'number',
          max: 10000000,
          default: 0,
          reply: () => this.R.equals(0, this.dataForm['boxSupplyWay']),
          selectCallback: this.numberChanged
        },
        scrapNumber: {
          label: '零头',
          type: 'number',
          max: 1000000,
          default: 0,
          selectCallback: this.numberChanged
        },
        countProductNumber: {
          label: '总数量',
          type: 'number',
          max: 100000000,
          default: 0,
          selectCallback: this.numberChanged
        },
        putInTime: {
          label: '入库时间',
          type: 'date',
          placeholder: '请选择入库时间'
        },
        location: {
          label: '存放位置',
          type: 'list',
          list: [],
          placeholder: '请输入存放位置'
        },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    productChanged() {
      const productId = this.R.propOr(0, 'productId', this.dataForm)

      if (this.R.isNil(productId) || this.R.isEmpty(productId)) {
        return
      }
      mRequest('productApi/productInfoInfo', {id: productId}, ({data}) => {
        this.dataForm.productWeight = this.R.pathOr(0, ['productInfo', 'productWeight'], data)
       // this.dataForm.boxId = this.R.pathOr(null, ['productInfo', 'boxId'], data)
      //  this.dataForm.trayInfoId = this.R.pathOr(null, ['productInfo', 'boxId'], data)
      })
    },
    orderDetailIdChanged() {
      const purchaseProductId = this.R.propOr(0, 'purchaseProductId', this.dataForm)

      if (this.R.isNil(purchaseProductId) || this.R.isEmpty(purchaseProductId)) {
        return
      }

      mRequest('purchaseApi/purchasePartInfo', {id:purchaseProductId}, (data) => {
        const purchaseProduct = this.R.pathOr({}, ['data', 'purchaseProduct'], data)
        this.dataForm.productId = purchaseProduct.productId
        this.dataForm.productWeight = purchaseProduct.productWeight
        this.dataForm.supplierId = purchaseProduct.supplierId
        this.dataForm.boxSupplyWay = this.R.propOr(0, 'boxSupplyWay',purchaseProduct.boxSupplyWay)

      })
    },

    productWeightChanged() {
      this.numberChanged()
    },
    numberChanged() {
      const packType = this.R.propOr(0, 'boxSupplyWay', this.dataForm)
      const productWeight = this.R.propOr(0, 'productWeight', this.dataForm)

      if (packType === 0) {
        const boxNumber = this.R.propOr(0, 'boxNumber', this.dataForm)
        const boxZhiNumber = this.R.propOr(0, 'boxZhiNumber', this.dataForm)
        const scrapNumber = this.R.propOr(0, 'scrapNumber', this.dataForm)

        this.dataForm.countProductNumber = boxNumber * boxZhiNumber + scrapNumber
      } else if (packType === 1) {
        const trayLayerNumber = this.R.propOr(0, 'trayLayerNumber', this.dataForm)
        const trayEachZhiNumber = this.R.propOr(0, 'trayEachZhiNumber', this.dataForm)
        const trayNumber = this.R.propOr(0, 'trayNumber', this.dataForm)
        const scrapNumber = this.R.propOr(0, 'scrapNumber', this.dataForm)

        this.dataForm.countProductNumber = trayLayerNumber * trayEachZhiNumber * trayNumber + scrapNumber
      //  this.dataForm.countProductNumber = trayLayerNumber * trayEachZhiNumber  + scrapNumber
      }
      this.dataForm.weightCount = this.dataForm.countProductNumber * productWeight
    },
    addNewProduct() {
      const cb = (newValue) => {
        this.initProductInfoList()
        this.dataForm.productId = newValue.id
      }
      this.$parent.openProductInfoDialog(cb)
    },
    addNewClientInfo(){
      const cb = (newValue) => {
        this.initClientInfoList()
        this.dataForm.clientId = newValue.id
      }
      this.$parent.openClientInfoDialog(cb)

    },
    init(id) {
      this.dataForm.id = id || 0
      this.initProductInfoList()
      this.initPutInStorageLocationList('location')
      // this.initBoxInfoIdList('boxId')
      // this.initTrayList()
      this.initPurchaseProductInfoList('purchaseProductId')
      this.initSupplierNoList()
      this.initSysUserInfoList('businessId')

      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule('productId', 'zhiNumber', 'putInTime','location', 'status'),
        initApi: 'productApi/productPutInStorageInfo',
        saveApi: 'productApi/saveProductPutInStorage',
        updateApi: 'productApi/updateProductPutInStorage',
        objectName: 'productPutInStorage'
      })
    }
  }
}
</script>
